package perso.perso.view.admin.school;

import java.io.IOException;
import java.util.List;

import org.apache.http.client.ClientProtocolException;

import com.sun.tools.internal.xjc.reader.xmlschema.bindinfo.BIConversion.User;

import perso.perso.MyAction;
import perso.perso.wsclient.HttpMiddlewareSchoolWSCall;
import perso.perso.wsclient.HttpMiddlewareUserWSCall;

public class AddClassAction extends MyAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private BeanClass beanClass;



	public BeanClass getBeanClass() {
		return beanClass;
	}



	public void setBeanClass(BeanClass beanClass) {
		this.beanClass = beanClass;
	}



	@Override
	public String execute() throws Exception {
		HttpMiddlewareSchoolWSCall h = new HttpMiddlewareSchoolWSCall();
		h.addClass(beanClass);

		return SUCCESS;
	}



	@Override
	public void validate() {

		if(beanClass.getLibelle() == null || beanClass.getLibelle().length() < 1 || "".equals(beanClass.getLibelle())){
			addFieldError( "beanClass.libelle", "le libelle est obligatoire" );
		}
		
		
		//recherche du nom
		HttpMiddlewareSchoolWSCall h = new HttpMiddlewareSchoolWSCall();
		try {
			
			List<BeanClass> classes = h.getAllClassesByFilters(beanClass);
			if(classes != null && classes.size() > 0){
				addActionError( "cette classe existe deja" );
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			System.out.println(e.getMessage());
		}
	}



}
